package com.bianmaba.oauth2.controller.order;

import com.bianmaba.oauth2.ExampleApplication;
import com.bianmaba.oauth2.bean.entity.Commodity;
import com.bianmaba.oauth2.bean.entity.Order;
import com.bianmaba.oauth2.service.order.IOrderService;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;

import java.util.UUID;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {ExampleApplication.class})
@AutoConfigureMockMvc
@Transactional
@WithMockUser(username = "admin", roles = {"USER", "ADMIN", "abc"})
public class OrderControllerTester {

    @Autowired
    private MockMvc mvc;
    @Autowired
    private IOrderService orderService;
    private Order order;

    @Before
    public void before() throws Exception {
        order = new Order();
        order.setName("name");

        Commodity commodity = new Commodity();
        commodity.setName("name");
        order.getCommodities().add(commodity);

        order = orderService.save(order);
        Assert.assertNotNull(order);
        Assert.assertNotNull(order.getPk());
    }

    @Test
    public void save() throws Exception {
        String pk = UUID.randomUUID().toString();
        LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("entity.name", "测试订单");
        params.add("entity.commodities[0].name", "苹果");
        params.add("entity.commodities[0].price", "123.23");
        params.add("entity.commodities[1].name", "牛奶糖");
        params.add("entity.commodities[1].price", "123.23");

        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/order/save");
        builder.params(params);
        builder.accept(MediaType.APPLICATION_JSON_UTF8);

        ResultActions resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
        //打印信息
        resultActions.andDo(MockMvcResultHandlers.print());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.success", Matchers.is(true)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.name", Matchers.is("测试订单")));

        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.commodities[*].name", Matchers.containsInAnyOrder("牛奶糖", "苹果")));
    }
}
